//=======================================================
//AUTHOR: JEFF XL. ZHANG ARIZONA STATE UNIVERSITY
//ALL RIGHTS RESERVED  2008
//=======================================================
#pragma once
#include<stdlib.h>
#include<iostream>
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
#include "line.h"
#include "detection.h"
#include "display.h"
#include "line_feature.h"
#include "image_feature.h"
#include "data_structure.h"
#include "feature_comparison.h"
#include "line_database.h"

using namespace std;

bool DEBUG = 1;

void extraction_process(vector<Line> lines, IplImage *img, ImageFeature& aframe){

	aframe.set_image(img);
	aframe.set_lines(lines);
	LineFeatureDetector alinefeaturedetector;
	Histogram hist;
//	for(int i = 0; i < 1; i++){		//FOR TESTING PURPOSE
	for(int i = 0; i < lines.size(); i++){		//FOR EACH LINE
		alinefeaturedetector.extract_line_featrue(img,lines[i],5);
//		alinefeaturedetector.verticalness?Num_V++:Num_H++;
		aframe.NUM_V += (int)alinefeaturedetector.verticalness;
		aframe.NUM_H += 1-(int)alinefeaturedetector.verticalness;
//		cout<<alinefeaturedetector.verticalness<<endl;
		
	//HISTOGRAM
		hist = alinefeaturedetector.compute_profile_histogram_array(img,lines[i],10,0);
		hist.visualize(1,1,i);	// 5,3 MOJO NUMBER

		aframe.Hists.push_back(hist);
	}
	cout<<"Horizonta Lines: "<<aframe.NUM_H<<"	Vertical Lines: "<<aframe.NUM_V<<endl;
}



void main(void){
	char filename_target[300],filename_anchor[300];
	char directory[300] = "C:/Documents and Settings/Jeff/My Documents/Data/Robot/Cubic_Nov/CLIP_20081104-222809/";
	int offset = DEBUG?5:10;
	int loop_for_frames = DEBUG?1:100;
	int time_interval = DEBUG?0:80;
	int increment = DEBUG?1:5;
	int start_point = 1600;		//GOOD:1597
	LineDatabase linedb;

	for(int i = 0; i < loop_for_frames; i++){

	//	char filename_anchor[300] = "C:/Documents and Settings/Jeff/My Documents/My Pictures/QuickCam/Picture 22.jpg";
	//	char filename_target[300] = "C:/Documents and Settings/Jeff/My Documents/My Pictures/QuickCam/Picture 23.jpg";
	//	char filename_target[300] = "C:/Documents and Settings/Jeff/My Documents/Data/Robot/Cubic_Nov/CLIP_20081104-222809/frame3364.jpg";
	//	char filename_anchor[300] = "C:/Documents and Settings/Jeff/My Documents/Data/Robot/Cubic_Nov/CLIP_20081104-222809/frame3318.jpg";
		sprintf(filename_target,"%sframe%d.jpg",directory,start_point +i*increment);
		sprintf(filename_anchor,"%sframe%d.jpg",directory,start_point +i*increment+offset);
	//	IplImage *img = cvLoadImage(filename,1);
		IplImage *img_anchor = cvLoadImage(filename_anchor,1);
		IplImage *img_target = cvLoadImage(filename_target,1);
	//	Display("img",img);	
		Display("anchor",img_anchor);	
		Display("target",img_target);	

		//Detection
		vector<Line> lines;
		vector<Line> lines_anchor, lines_target;
		Detection adetection;
	//	lines = adetection.DetectLine(img);
		lines_anchor = adetection.DetectLine(img_anchor,"anchor");
		lines_target = adetection.DetectLine(img_target,"target");

		LineFeature afeature;
		ImageFeature aframe;
		ImageFeature frame_anchor, frame_target;


	//	extraction_process(lines,img,aframe);
		extraction_process(lines_anchor,img_anchor,frame_anchor);
		extraction_process(lines_target,img_target,frame_target);
		
	//	Display("demo_anchor",frame_anchor.get_image());
		//Parameterization
		//INDICATE THE RESULT SOMEHOW	
		//FEATURE COMPARISON
		FeatureComparison acomparison;
		acomparison.compare(frame_anchor,frame_target);		//INPUT TWO FRAMES 

		linedb.set_lines_for_frame(lines);

		cvReleaseImage(&img_anchor);
		cvReleaseImage(&img_target);

		cvWaitKey(time_interval);
	}




}	
